module Kuztuscms
  class Settings
    class << self
      def set(key, value)
        Kuztuscms::Setting.create!(:key => key, :value => value)
        self.clear_cache
      end

      def clear_cache
        @cache = nil
      end

      def get(key)
        unless @cache
          @cache = Kuztuscms::Setting.all.inject({}) do |res, setting|
            res[setting.key] = setting.value

            res
          end
        end

        @cache[key]
      end
    end
  end
end
